package com.renge.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.renge.eduservice.entity.EduChapter;
import com.renge.eduservice.entity.EduVideo;
import com.renge.eduservice.entity.json.ChapterJson;
import com.renge.eduservice.entity.json.VideoJson;
import com.renge.eduservice.mapper.EduChapterMapper;
import com.renge.eduservice.service.EduChapterService;
import com.renge.eduservice.service.EduVideoService;
import com.renge.exception.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-06-04
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;
    //返回课程的章节和其下小结
    @Override
    public List<ChapterJson> listChapterVideo(String courseId) {
        //得到EduChapter集合
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        queryWrapper.orderByAsc("gmt_create");
        List<EduChapter> chapterlist = this.list(queryWrapper);

        if(chapterlist.size()<=0){
            GuliException guliException = new GuliException();
            guliException.setCode(20001);
            guliException.setMsg("注意：该课程没有章节");
            throw guliException;
        }
        //得到EduVideo集合
        QueryWrapper<EduVideo> query = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        List<EduVideo> videolist = eduVideoService.list(query);

        //循环EduChapter集合得到ChapterJson集合
        List<ChapterJson>cjs=new ArrayList<>();
        for(EduChapter ec:chapterlist){
            //封装chapterJson
            ChapterJson chapterJson = new ChapterJson();
            chapterJson.setId(ec.getId());
            chapterJson.setLabel(ec.getTitle());
            ArrayList<VideoJson>list=new ArrayList<>();
            //封装chapterJson子类
            for(EduVideo ev:videolist){
                if(ev.getChapterId().equals(ec.getId())){
                    VideoJson videoJson = new VideoJson();
                    videoJson.setId(ev.getId());
                    videoJson.setLabel(ev.getTitle());
                    videoJson.setVideoSourceId(ev.getVideoSourceId());
                    list.add(videoJson);
                }
            }
            chapterJson.setChildren(list);
            cjs.add(chapterJson);
        }

        return cjs;
    }

    @Override
    @Transactional
    public boolean deleteChapterVideo(String chapterId) {
        //查询是否存在章节
        QueryWrapper<EduChapter> Query = new QueryWrapper<>();
        Query.eq("id",chapterId);
        EduChapter chapter = this.getOne(Query);
        if(chapter==null){
            GuliException guliException = new GuliException();
            guliException.setCode(20001);
            guliException.setMsg("注意：该课程没有章节");
            throw guliException;
        }
        //查询章节是否有
        QueryWrapper<EduVideo> QueryWrapper = new QueryWrapper<>();
        QueryWrapper.eq("chapter_id",chapterId);
        int count = eduVideoService.count(QueryWrapper);
        if(count>0){
            GuliException guliException = new GuliException();
            guliException.setCode(20001);
            guliException.setMsg("此章节有小节");
            throw guliException;
        }
       return this.removeById(chapterId);

    }

    @Override
    public EduChapter listChapterVideobyChapterId(String chapterId) {
        EduChapter byId = this.getById(chapterId);
        return byId;
    }
}
